package com.example.controller;

import com.example.entity.CurrentUser;
import com.example.entity.InStore;
import com.example.entity.Result;
import com.example.entity.Store;
import com.example.page.Page;
import com.example.service.InStoreService;
import com.example.service.StoreService;
import com.example.utils.TokenUtils;
import com.example.utils.WarehouseConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author:tjf
 * @Description:
 */
@RequestMapping("/instore")
@RestController
public class InStoreController {
    @Autowired
    private StoreService storeService;
    @Autowired
    private InStoreService inStoreService;
    @Autowired
    private TokenUtils tokenUtils;
    /**
     * 获取仓库列表接口url/instore/store-list
     * @return
     */
    @RequestMapping("/store-list")
    public Result getStoreList(){
        // 执行查询所有仓库的业务方法
        List<Store> storeList = storeService.queryAllStore();

        return Result.ok(storeList);
    }

    /**
     * 根据条件查询入库单并分页接口url/instore/instore-page-list
     * @param inStore
     * @param page
     * @return
     */
    @RequestMapping("/instore-page-list")
    public Result queryInStoreByConditionForPagination(InStore inStore, Page page){
        // 执行查询入库单并分页的业务方法
        inStoreService.queryInStoreByConditionForPagination(inStore, page);

        // 响应
        return Result.ok(page);
    }

    /**
     * 确定入库接口url/instore/instore-confirm
     * @param inStore
     * @param token
     * @return
     */
    @RequestMapping("/instore-confirm")
    public Result confirmInStore(@RequestBody InStore inStore,
                     @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token){
        //1 获取当前登录用户的信息
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);

        //2 执行确定入库的业务方法
        Result result = inStoreService.confirmInStore(inStore, currentUser.getUserId());

        //3 响应
        return result;
    }
}
